import 'package:flutter/material.dart';
import 'package:flutter_kit_dsp/flutter_kit_dsp.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

class SearchItem extends StatefulWidget {
  final DSPeripheral peripheral;
  final void Function() onClick;
  final void Function() onLongPress;
  SearchItem({super.key, required this.peripheral, required this.onClick, required this.onLongPress});

  @override
  State<SearchItem> createState() => _SearchItemState();
}

class _SearchItemState extends State<SearchItem> {
  Widget buildText(String text, bool isConnect) {
    return Obx(() {
      bool connect = widget.peripheral.peripheral?.result.device.isConnected ?? false;
      connect = BluetoothManager.instance.isConnectedRx && connect;
      return InkWell(
        onTap: () => isConnect ? widget.onClick() : null,
        onLongPress: () => isConnect ? null : widget.onLongPress(),
        child: Text(
          text,
          style: TextStyle(fontSize: connect ? 18 : 14, color: connect ? Colors.red : Colors.white),
        ),
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 40,
      child: Column(children: [
        Expanded(
          child: Padding(
            padding: EdgeInsets.only(left: 10, right: 10),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Expanded(child: buildText(widget.peripheral.peripheral!.result.device.advName, true)),
                buildText(widget.peripheral.name, false),
              ],
            ),
          ),
        ),
        SizedBox(height: 0.5, child: Container(color: Colors.white))
      ]),
    );
  }
}
